package com.oa.modules.approvalsec1.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.oa.common.utils.AppUtils;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.R;
import com.oa.modules.approvalsec1.entity.AssistInManagingEntity;
import com.oa.modules.approvalsec1.entity.NewsNoticeEntity;
import com.oa.modules.approvalsec1.entity.ToDoImmediatelyEntity;
import com.oa.modules.approvalsec1.service.AssistInManagingService;
import com.oa.modules.approvalsec1.service.NewsNoticeService;
import com.oa.modules.approvalsec1.service.ToDoImmediatelyService;
import com.oa.modules.sys.controller.AbstractController;
import com.oa.modules.sys.entity.SysUserEntity;
import com.oa.modules.sys.service.SysUserRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.oa.modules.approvalsec1.entity.UserAssignEntity;
import com.oa.modules.approvalsec1.service.UserAssignService;



/**
 *
 *
 * @author jiangwei
 * @email 785297226@qq.com
 * @date 2021-03-23 16:50:35
 */
@RestController
@RequestMapping("user/assign")
@Api(tags = "分派中间表相关接口")
public class UserAssignController extends AbstractController {
    @Autowired
    private UserAssignService userAssignService;

    @Autowired
    private AssistInManagingService assistInManagingService;

    @Autowired
    private ToDoImmediatelyService toDoImmediatelyService;

    @Autowired
    private NewsNoticeService newsNoticeService;

    @Autowired
    private SysUserRoleService userRoleService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userAssignService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "信息")
    public R info(@PathVariable("id") String id){
		UserAssignEntity userAssign = userAssignService.getById(id);
        return R.ok().put("userAssign", userAssign);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation(value = "保存")
    public R save(@RequestBody UserAssignEntity userAssign){
		if(userAssign.getMatterType().equals("1")){ //帮办的
            AssistInManagingEntity assistInManaging = assistInManagingService.getOne(new QueryWrapper<AssistInManagingEntity>().eq("id", userAssign.getMatterId()));
            if(assistInManaging != null){
                userAssign.setId(AppUtils.getUUID());
                userAssign.setCreateDate(new Date());
                userAssign.setCreateOper(getUserId().toString());
                userAssignService.save(userAssign);
                assistInManaging.setIsAssign("1");
                assistInManaging.setAssignDate(new Date());
                assistInManaging.setProgressBar("1");
                assistInManagingService.updateById(assistInManaging);
                NewsNoticeEntity noticeEntity = new NewsNoticeEntity();
                noticeEntity.setId(AppUtils.getUUID());
                noticeEntity.setCreateDate(new Date());
                noticeEntity.setCreateOper(getUserId().toString());
                noticeEntity.setMatterId(userAssign.getMatterId());
                noticeEntity.setMatterType(userAssign.getMatterType());
                noticeEntity.setPushUserId(userAssign.getOperatorId());
                newsNoticeService.save(noticeEntity);
                SysUserEntity user = sysUserService.getById(userAssign.getOperatorId());
                if(user != null && null != user.getWxOpenId()){
                    String response = WxLoginController.noticePush(user.getWxOpenId(),"帮办","已分派，待处理", new Date(),assistInManaging.getCreateDate());
                    System.err.println(response);
                }
                return R.ok();
            }else{
                return R.error("暂无该帮办任务");
            }
        }else if(userAssign.getMatterType().equals("1")){ //企业立即办的
            ToDoImmediatelyEntity toDoImmediately = toDoImmediatelyService.getOne(new QueryWrapper<ToDoImmediatelyEntity>().eq("id", userAssign.getMatterId()));
            if(toDoImmediately != null){
                userAssign.setId(AppUtils.getUUID());
                userAssign.setCreateDate(new Date());
                userAssign.setCreateOper(getUserId().toString());
                userAssignService.save(userAssign);
                toDoImmediately.setIsAssign("1");
                toDoImmediately.setAssignDate(new Date());
                toDoImmediately.setProgressBar("1");
                toDoImmediatelyService.updateById(toDoImmediately);
                NewsNoticeEntity noticeEntity = new NewsNoticeEntity();
                noticeEntity.setId(AppUtils.getUUID());
                noticeEntity.setCreateDate(new Date());
                noticeEntity.setCreateOper(getUserId().toString());
                noticeEntity.setMatterId(userAssign.getMatterId());
                noticeEntity.setMatterType(userAssign.getMatterType());
                noticeEntity.setPushUserId(userAssign.getOperatorId());
                newsNoticeService.save(noticeEntity);
                SysUserEntity user = sysUserService.getById(userAssign.getOperatorId());
                if(user != null  && null != user.getWxOpenId()){
                    String response = WxLoginController.noticePush(user.getWxOpenId(),"立即办","已分派，待处理", new Date(),toDoImmediately.getCreateDate());
                    System.err.println(response);
                }
                return R.ok();
            }else{
                return R.error("暂无该立即办任务");
            }
        }
        return R.error("请检查是否是帮办或者企业立即办的事项！");
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@RequestBody UserAssignEntity userAssign){
        userAssign.setUpdateDate(new Date());
        userAssign.setUpdateOper(getUserId().toString());
		userAssignService.updateById(userAssign);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    public R delete(@RequestBody String[] ids){
		userAssignService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
